#!/usr/bin/env python
# encoding: utf-8
"""
thingslog.py

Created by Christopher Glasman on 2009-04-23.
Copyright (c) 2009 Christopher Glasman. 

This file is part of thingsplot.

thingsplot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

thingsplot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with thingsplot.  If not, see <http://www.gnu.org/licenses/>.
"""

import sys
import os
from ThingsDatabase import ThingsDatabase



def main(database, hour, outfile=None):
    # load and do an initial parse of the Things XML Database.
    db = ThingsDatabase(database)
    # loop through the database to find the defined Projects.
    db.find_projects()
    #
    # loop through the database and attach Todo items to projects and
    # record timestamp data. 
    db.find_todo_items()
    #
    # Find items completed today since starting hour "hour"
    # default 6am. These can be printed out to a file also. 
    if(outfile):
        db.print_daily_report(hour, outfile)
    else:
        db.print_daily_report(hour)
    return


if __name__ == '__main__':
    try:
        if(len(sys.argv) == 1):
            # print "case 1"
            home = os.path.expanduser("~")
            file = home + '/Library/Application Support/Cultured Code/Things/Database.xml'
            main(file, 6)
            sys.exit()
        elif(len(sys.argv) == 2):
            # print "case 2"
            file = sys.argv[1]
            main(file, 6)
            sys.exit()
        if(len(sys.argv) == 3):
            # print "case 3"
            file = sys.argv[1]
            start_hour = int(sys.argv[2])
            main(file, start_hour)
            sys.exit()
        if(len(sys.argv) == 4):
            # print "case 4"
            file = sys.argv[1]
            start_hour = int(sys.argv[2])
            outfile = sys.argv[3]
            main(file, start_hour, outfile)
            sys.exit()
        #we shouldn't be here...
        print "syntax is: thingslog.py [/path/to/Database.xml] [starting_hour] [/path/to/logfile]"
        sys.exit()
    except IOError:
        print "syntax is: thingslog.py [/path/to/Database.xml] [starting_hour] [/path/to/logfile]"
        raise
    except:
        raise

